﻿using K9Nano.Application.Authentication;
using K9Nano.Authorization;
using K9Nano.Domain.Entities;
using Microsoft.AspNetCore.Mvc;

namespace K9Nano.AspNetCore.Controllers;

public abstract class RoleControllerBase<TRole, TService>(TService service)
    : CurdControllerBase<TRole, TService, RoleCreateDto, RoleEditDto, RoleDto, RoleQueryDto<TRole>>(service)
    where TRole : RoleBase
    where TService : IRoleAppServiceBase<TRole>
{
    [HttpGet("permissions")]
    public virtual IEnumerable<Permission> GetAllPermissions([FromServices] IPermissionCatalog catalog)
    {
        return catalog.Permissions;
    }

    [HttpPost("permissions")]
    public virtual async Task AddPermissions(RolePermissionEditDto dto)
    {
        await service.AddPermissionsAsync(dto, HttpContext.RequestAborted);
    }

    [HttpDelete("permissions")]
    public virtual async Task RemovePermissions(RolePermissionEditDto dto)
    {
        await service.AddPermissionsAsync(dto, HttpContext.RequestAborted);
    }

    /// <summary>
    /// 获取角色的权限列表
    /// </summary>
    /// <param name="id">角色ID</param>
    /// <returns></returns>
    [HttpGet("permissions/{id}")]
    public virtual async Task<IEnumerable<Permission>> GetPermissionsByRoleId(long id)
    {
        return await service.GetPermissionsByRoleIdAsync(id, HttpContext.RequestAborted);
    }
}